<!doctype html>
<html lang="en">
<head>
    @include('web.layout.head')
    <link rel="stylesheet" href="{{asset('/frame/jedate/jedate.css')}}">
    <script src="{{asset('/frame/jedate/jedate.js')}}"></script>
    <script src="{{mix('/js/web/frame/distpicker.js')}}"></script>
    <link rel="stylesheet" href="{{mix('/css/web/user/matching.css')}}">
</head>
<body>
@include('web.layout.header')
@include('web.layout.user_tab')
<div class="base_member_content">
    <div class="w1200">
        @include('web.layout.seller_nav')
        <div class="nav_right flr">
            <form method="post" id="postForm"  enctype ="multipart/form-data">
                <div class="base_content">
                    <div class="line_title">新增项目配单</div>
                    <div class="sales_info">
                        <div class="sales_info_left fl">
                            <h2>配单购买方信息<font>（必填）</font></h2>
                            <input name="buyer_phone" id="buyer_phone" type="text" value="{{!empty($item_clone)?$item_clone['buyer_phone']:''}}" placeholder="手机号" required>
                            <input name="buyer_company" id="buyer_company" type="text" value="{{!empty($item_clone)?$item_clone['buyer_company']:''}}" placeholder="公司名称" >
                            <input name="buyer_contact" id="buyer_contact" type="text" value="{{!empty($item_clone)?$item_clone['buyer_contact']:''}}" placeholder="联系人姓名" required>
                        </div>
                        <div class="sales_info_left2 fl">
                            <h2>选择报价要素<font>（必填）</font></h2>
                            <div class="price_box">
                                <span>价格包含：</span>
                                <label><input type="checkbox" name="quote_contain[]" value="1" {{!empty($item_clone) && in_array(1,$item_clone['quote_contain'])?'checked':''}}>票税</label>
                                <label><input type="checkbox" name="quote_contain[]" value="2" {{!empty($item_clone) && in_array(2,$item_clone['quote_contain'])?'checked':''}}>运费</label>
                                <label><input type="checkbox" name="quote_contain[]" value="3" {{!empty($item_clone) && in_array(3,$item_clone['quote_contain'])?'checked':''}}>施工安装</label>
                                <label><input type="checkbox" name="quote_contain[]" value="4" {{!empty($item_clone) && in_array(4,$item_clone['quote_contain'])?'checked':''}}>公报总价</label>
                            </div>
                            <div class="delivery_time_box">
                                <span>交货时间：</span>
                                <label><input type="radio" name="delivery_time" value="1" {{!empty($item_clone) && $item_clone['quote_trade'][0]==1?'checked':''}}>今天</label>
                                <label><input type="radio" name="delivery_time" value="3" {{!empty($item_clone) && $item_clone['quote_trade'][0]==3?'checked':''}}>3天</label>
                                <label><input type="radio" name="delivery_time" value="7" {{!empty($item_clone) && $item_clone['quote_trade'][0]==7?'checked':''}}>7天</label>
                                <label><input type="radio" name="delivery_time" value="0" {{!empty($item_clone) && $item_clone['quote_trade'][0]==0?'checked':''}}>其他</label>
                                <input name="otherTime" placeholder="请选择时间" class="otherTime" id="otherTime" value="{{!empty($item_clone) && $item_clone['quote_trade'][0]==0?$item_clone['quote_trade'][1]:''}}" style="{{!empty($item_clone) && $item_clone['quote_trade'][0]==0?'display: inline':''}}">
                            </div>
                            <div class="project_location_box">
                                <span>项目地点：</span>
                                <div id="distpicker">
                                    <select class="province" name="province_id">
                                        <option>请选择省份</option>
                                    </select>
                                    <input type="hidden" name="province_text" id="province_text">
                                    <select class="city" name="city_id">
                                        <option>请选择城市</option>
                                    </select>
                                    <input type="text" name="address" class="address" required value="{{!empty($item_clone)?$item_clone['project_addr']:''}}" placeholder="请输入详细地址">
                                    <input type="hidden" name="city_text" id="city_text">
                                </div>
                            </div>
                        </div>
                        <div class="sales_info_right fr">
                            <h2>配单销售方信息</h2>
                            <input name="seller_phone" type="text" placeholder="手机号" value="{{$data['phone']}}">
                            <input name="seller_contact"  type="hidden" placeholder="卖家联系人" value="{{$data['name']}}">
                            <input name="seller_company"  type="hidden" placeholder="卖家公司" value="{{$data['company']['company_name']}}">
                            <span>{{$data['name']}}</span>
                            <span>{{$data['company']['company_name']}}</span>
                        </div>
                    </div>
                    <div class="matching_h3 fl">产品配单</div>
                    <div class="product_list_main fl">
                        <div class="product_list_th_box fl">
                            <span class="product_list_th1">序号</span>
                            <span class="product_list_th2">品名</span>
                            <span class="product_list_th3">品牌</span>
                            <span class="product_list_th4">型号</span>
                            <span class="product_list_th5">数量</span>
                            <span class="product_list_th6">单价（元）</span>
                            <span class="product_list_th7">合计（元）</span>
                            <span class="product_list_th8">操作</span>
                        </div>
                        <div class="append_main fl">
                            @if(!empty($item_clone))
                                @if(!empty($item_clone['items_products']))
                                    @foreach($item_clone['items_products'] as $k =>$v)
                                        @if($v['type']==1)
                                        {{--商品--}}
                                        <div class="append_goods_box">
                                            <span class="append_goods_box_tr1">{{$k+1}}</span>
                                           <span class="append_goods_box_tr2">
                                               <input type="text" name="append_goods_name[]" value="{{$v['product_name']}}" placeholder="品名">
                                           </span>
                                            <span class="append_goods_box_tr3">
                                               <input type="text" name="append_goods_brand[]" value="{{$v['brand']}}" placeholder="品牌">
                                            </span>
                                            <span class="append_goods_box_tr4">
                                               <input type="text" name="append_goods_model[]" value="{{$v['model']}}" placeholder="型号">
                                            </span>
                                            <span class="append_goods_box_tr5">
                                               <div class="minus fl">-</div>
                                               <input class="fl" type="text" name="append_goods_num[]" value="{{$v['num']}}">
                                               <div class="plus fl">+</div>
                                            </span>
                                            <span class="append_goods_box_tr6">
                                               <input type="text" name="append_goods_univalent[]" value="{{$v['price']}}" placeholder="单价" onkeyup="numUnivalent(this)">
                                            </span>
                                            <span class="append_goods_box_tr7">{{$v['total_price']}}</span>
                                            <span class="append_goods_box_tr8">
                                               <img class="del_btn" src="/img/web/user/del_icon.png">
                                            </span>
                                        </div>

                                        @else
                                        {{--费用--}}
                                        <div class="append_cost_box">
                                            <span class="append_cost_box_tr1">{{$k+1}}</span>
                                            <span class="append_cost_box_tr2">
                                                <input type="text" name="append_cost_name[]" value="{{$v['product_name']}}" placeholder="费用名称">
                                            </span>
                                            <span class="append_cost_box_tr3">
                                                <div class="minus fl">-</div>
                                                <input class="fl" type="text" name="append_cost_num[]" value="{{$v['num']}}">
                                                <div class="plus fl">+</div>
                                            </span>
                                            <span class="append_cost_box_tr4">
                                                <input type="text" name="append_cost_univalent[]" value="{{$v['price']}}" placeholder="单价" onkeyup="numUnivalent(this)">
                                            </span>
                                            <span class="append_cost_box_tr5">{{$v['total_price']}}</span>
                                            <span class="append_cost_box_tr6">
                                                <img class="del_btn" src="/img/web/user/del_icon.png">
                                            </span>
                                        </div>

                                        @endif
                                    @endforeach
                                @endif
                            @endif


                        </div>



                        <div class="append_btn_main fl">
                            <div class="append_goods_btn"><font>+</font><span>追加商品</span></div>
                            <div class="append_cost_btn"><font>+</font><span>追加费用</span></div>
                        </div>
                    </div>
                    <div class="price_calculate_main fr">
                        <span>商品总价：</span><div class="price_calculate_main_fr fl">￥<font class="gooos_total_price">{{!empty($item_clone)?$item_clone['goods_total']:'0.00'}}</font></div>
                        <span>其它费用：</span><div class="price_calculate_main_fr fl">￥<font class="other_charges">{{!empty($item_clone)?$item_clone['other_total']:'0.00'}}</font></div>
                        <span>总价：</span><div class="price_calculate_main_fr2 fl">￥<font class="total_price">{{!empty($item_clone)?$item_clone['all_total']:'0.00'}}</font></div>
                    </div>
                </div>
                <div class="generating_sheet_main fl">
                    <input type="button" class="tijiao" value="生成配单">
                    <label><input type="checkbox" name="sms_notify" value="1" {{!empty($item_clone) && $item_clone['sms_notify']==1?'checked':''}}>短信通知</label>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="bg_h"></div>
@include('web.layout.footer')
<script type="text/javascript">
     jeDate('#otherTime',{
        format:"YYYY-MM-DD",
        minDate: jeDate.nowDate(0)
    });
    function numUnivalent(obj){
        obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
        obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字
        obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
        obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
    }
    $(function(){
        var province = "";
        var city = "";
        $('#distpicker').distpicker({
            province: province,
            city: city
        });

        $('.province').change(function(){
            var province_text=$(".province option:selected").text();
            $('#province_text').val(province_text);
        });
        $('.city').change(function(){
            var city_text=$(".city option:selected").text();
            $('#city_text').val(city_text);
        });
        //交货时间--其他
        $('.delivery_time_box label input').click(function(){
            var type = $(this).val();
            if(type == 0){
                $('.otherTime').show();
            }else{
                $('.otherTime').hide();
            }
        });

        //追加商品
        $('.append_goods_btn').click(function(){
            var num = $('.append_main > div').length+1;
            var html = '<div class="append_goods_box">'
                            +'<span class="append_goods_box_tr1">'+num+'</span>'
                            +'<span class="append_goods_box_tr2">'
                                +'<input type="text" name="append_goods_name[]" value="" placeholder="品名">'
                            +'</span>'
                            +'<span class="append_goods_box_tr3">'
                                +'<input type="text" name="append_goods_brand[]" value="" placeholder="品牌">'
                            +'</span>'
                            +'<span class="append_goods_box_tr4">'
                                +'<input type="text" name="append_goods_model[]" value="" placeholder="型号">'
                            +'</span>'
                            +'<span class="append_goods_box_tr5">'
                                +'<div class="minus fl">-</div>'
                                +'<input class="fl" type="text" name="append_goods_num[]" value="1">'
                                +'<div class="plus fl">+</div>'
                            +'</span>'
                            +'<span class="append_goods_box_tr6">'
                                +'<input type="text" name="append_goods_univalent[]" value="0" placeholder="单价" onkeyup="numUnivalent(this)">'
                            +'</span>'
                            +'<span class="append_goods_box_tr7">0.00</span>'
                            +'<span class="append_goods_box_tr8">'
                                /*+'<img class="eidt_btn" src="/img/web/user/eidt_icon.png">'*/
                                +'<img class="del_btn" src="/img/web/user/del_icon.png">'
                            +'</span>'
                            +'<ul class="prompt"></ul>'
                        +'</div>';
            $('.append_main').append(html);
        });

        //追加费用
        $('.append_cost_btn').click(function(){
            var num = $('.append_main > div').length+1;
            var html = '<div class="append_cost_box">'
                                +'<span class="append_cost_box_tr1">'+num+'</span>'
                                +'<span class="append_cost_box_tr2">'
                                    +'<input type="text" name="append_cost_name[]" value="" placeholder="费用名称">'
                                +'</span>'
                                +'<span class="append_cost_box_tr3">'
                                    +'<div class="minus fl">-</div>'
                                    +'<input class="fl" type="text" name="append_cost_num[]" value="1">'
                                    +'<div class="plus fl">+</div>'
                                +'</span>'
                                +'<span class="append_cost_box_tr4">'
                                    +'<input type="text" name="append_cost_univalent[]" value="0" placeholder="单价" onkeyup="numUnivalent(this)">'
                                +'</span>'
                                +'<span class="append_cost_box_tr5">0.00</span>'
                                +'<span class="append_cost_box_tr6">'
                                    /*+'<img class="eidt_btn" src="/img/web/user/eidt_icon.png">'*/
                                    +'<img class="del_btn" src="/img/web/user/del_icon.png">'
                                +'</span>'
                            +'</div>';
            $('.append_main').append(html);
        });
        //
        $('body').on('keyup','input[name="append_goods_name[]"]',function (e) {
            var _this = this;
            if($(_this).val()==''){
                $(_this).parent().parent().find('ul').hide();
            }else{
                var name = $(_this).val();
                /* html = '<li data-id="1" data-brand="防晒" data-price="20.25" data-model="型号">11222333</li>'
                            $(_this).parent().parent().find('ul').html(html).show();*/
                $.ajax({
                    url: '/member/seller/items/items_stock',
                    type: 'GET',
                    dataType: 'json',
                    data: {product_name: name},
                    success:function(data){
                        if(0==data.code){
                            var html = '';
                            if(data.data.length>0){
                                $.each(data.data, function(index, val) {
                                    html +='<li data-id="'+val.id+'" data-brand="'+val.brand+'" data-price="'+val.price+'" data-model="'+val.model+'">' + val.product_name + '</li>';
                                });
    //                            html = '<li data-brand="1" data-price="20.25">11222333</li>'
                                $(_this).parent().parent().find('ul').html(html).show();
                            }else{
                                html = '<p style="text-align: center;">未搜索到商品</p>'
                                $(_this).parent().parent().find('ul').html(html).show();
                            }

                        }

                    }
                });
            }
        });

        $('body').on('blur','input[name="append_goods_name[]"]',function (e) {
            setTimeout(function(){
                $('.prompt').hide();
            },500);
        })

        $('body').on('click','.prompt li',function(){
            var id = $(this).attr('data-id');
            var brand = $(this).attr('data-brand');
            var price = $(this).attr('data-price');
            var model = $(this).attr('data-model');
            var name = $(this).html();
            $(this).parent().parent().find('input[name="append_goods_name[]"]').val(name);
            $(this).parent().parent().find('input[name="append_goods_brand[]"]').val(brand);
            $(this).parent().parent().find('input[name="append_goods_model[]"]').val(model);
            $(this).parent().parent().find('input[name="append_goods_univalent[]"]').val(price);
            $(this).parent().parent().find('.append_goods_box_tr7').html(price);
            $(this).parent().hide();
            aggregate();
        })
        //商品--减
        $('body').on('click','.minus',function(){
            var num = parseInt($(this).next("input").val());
            var univalent = $(this).parent().next().find('input').val();
            if(num > 1){
                $(this).next("input").val(num - 1);
                $(this).parent().next().next().html(((num - 1)*univalent).toFixed(2));
            }
            aggregate();
        });

        //商品--加
        $('body').on('click','.plus',function(){
            var num = parseInt($(this).prev("input").val());
            var univalent = $(this).parent().next().find('input').val();
                $(this).prev("input").val(num + 1);
                $(this).parent().next().next().html(((num + 1)*univalent).toFixed(2));
            aggregate();
        });

        //商品--数量输入
        $('body').on('keyup','.append_goods_box_tr5 input,.append_cost_box_tr3 input',function(){
            var c=$(this);
            if(/[^\d]/.test(c.val())){//替换非数字字符
              var temp_amount=c.val().replace(/[^\d]/g,'');
              $(this).val(temp_amount);
            }
            if($(this).val() == ''){
                $(this).val(1);
            }
            var num = parseInt($(this).val());
            var univalent = $(this).parent().next().find('input').val();
            $(this).parent().next().next().html((num*univalent).toFixed(2));
            aggregate();
        })

        //商品--单价输入
        $('body').on('keyup','.append_goods_box_tr6 input,.append_cost_box_tr4 input',function(){
            var num = parseInt($(this).parent().prev().find('input').val());
            $(this).parent().next().html((num*$(this).val()).toFixed(2));
            aggregate();
        })

        //删除
        $('body').on('click','.del_btn',function(){
            if(confirm('你确定删除吗？')){
                $(this).parent().parent().remove();
                $('.append_main > div').each(function (index, el) {
                    $(this).find('span').first().html(index+1);
                    aggregate();
                });
            }
        });

        function aggregate(){
            var gooos_total_price =0;
            var other_charges =0;
            if($('.append_goods_box_tr7').length>0){
                $('.append_goods_box_tr7').each(function (index, el) {
                    gooos_total_price = (Number(gooos_total_price) + Number($(this).html())).toFixed(2);
                });
                $('.gooos_total_price').html(gooos_total_price);
            }else{
                $('.gooos_total_price').html('0.00');
            }

            if($('.append_cost_box_tr5').length>0){
                $('.append_cost_box_tr5').each(function (index, el) {
                    //other_charges = (Number(other_charges) + Number($(this).html()).toFixed(2));
                    other_charges = (Number(other_charges) + Number($(this).html())).toFixed(2);
                });
                $('.other_charges').html(other_charges);
            }else{
                $('.other_charges').html('0.00');
            }

            $('.total_price').html((Number(gooos_total_price)+Number(other_charges)).toFixed(2));
            //$('.total_price').html(((parseInt(gooos_total_price*100)+parseInt(other_charges*100))/100).toFixed(2));
        }

        //手机号对应公司和姓名
        $("#buyer_phone").change(function() {
            var uid = "{{session('user_id')}}";
            var buyer_phone=$('#buyer_phone').val();
            if(uid && buyer_phone){
                if(/^1\d{10}$/.test(buyer_phone)){
                    $.ajax({
                        url: '/member/seller/items/item_shop',
                        type: 'get',
                        data:{'buyer_phone':buyer_phone},
                        dataType:'json',
                        success: function (data) {
                            if(data.code==0){
                                $('#buyer_company').val(data.data.buyer_company);
                                $('#buyer_contact').val(data.data.name);
                                layer.alert("手机号码匹配成功!",{icon: 1});
                            }else{
                                layer.alert("手机号码匹配失败!",{icon: 0});
                            }
                        }
                    });
                }else{
                    layer.alert("手机号码格式不正确!",{icon: 0});
                }

            }

        });

        //生成配单
        $(".tijiao").on('click', function(event) {
            var isPhone = /(?:\(\d{3,4}\)|\d{3,4}-?)\d{8}/;//手机号码验证
            var buyer_phone = $("input[name='buyer_phone']").val();
            if (!isPhone.test(buyer_phone)) {
                layer.alert("请输入正确的配单购买方手机号!",{icon: 0});
                return false;
            }
//            var buyer_company = $("input[name='buyer_company']").val();
//            if (buyer_company == '') {
//                layer.alert("请输入配单购买方公司名称!",{icon: 0});
//                return false;
//            }
            var buyer_contact = $("input[name='buyer_contact']").val();
            if (buyer_contact == '') {
                layer.alert("请输入配单购买方联系人姓名!",{icon: 0});
                return false;
            }
            var quote_contain = $("input[name='quote_contain[]']:checked").length;
            if (quote_contain == 0) {
                layer.alert("请选择价格包含!",{icon: 0});
                return false;
            }
            var delivery_time = $("input[name='delivery_time']:checked").val();
            if (delivery_time ==null) {
                layer.alert("请选择交货时间!",{icon: 0});
                return false;
            }
            var otherTime = $("input[name='otherTime']").val();
            if (delivery_time ==0 && otherTime == '') {
                layer.alert("请选择其他交货时间!",{icon: 0});
                return false;
            }
            var province = $(".province option:selected").text();
            if (province == '请选择省份') {
                layer.alert("请选择项目地点省份!",{icon: 0});
                return false;
            }
             var city = $(".city option:selected").text();
            if (city == '请选择城市') {
                layer.alert("请选择项目地点城市!",{icon: 0});
                return false;
            }
            var seller_phone = $("input[name='seller_phone']").val();
            if (!isPhone.test(seller_phone)) {
                layer.alert("请输入正确的配单销售方手机号!",{icon: 0});
                return false;
            }
            if($('.append_main > div').length>0){
                var input_num = 0;
                $('.append_main input[type="text"]').each(function(){
                    if($(this).val() == ''){
                        input_num++;
                    }
                })
                if (input_num > 0){
                    layer.alert("请填写完整产品配单!",{icon: 0});
                    return false;
                }
            }else{
                layer.alert("请添加产品配单!",{icon: 0});
                return false;
            }

            var total_price = $(".total_price").html();
            if (total_price=='0.00') {
                layer.alert("配单价格不能为0!",{icon: 0});
                return false;
            }

            $.ajax({
                url: '/member/seller/items',
                type: 'post',
                data: $('#postForm').serialize(),
                dataType:'json',
                success: function (data) {
                    if(data.code==0){
                        layer.alert(data.message,{icon: 1});
                        window.location.href="/member/seller/items"
                    }else{
                        layer.alert(data.message,{icon: 0});
                    }
                }
            });


        });


    });

</script>
</body>
</html>